import React, {Component} from "react";

class Clock extends Component {
  constructor (props) {
    super(props);
    this.state = {
      date: new Date()
    };
  }

  componentDidMount () {
    this.timerId = setInterval(() => {
      this.tick()
    }, 1000)
  }

  componentWillUnmount () {
    clearInterval(this.timerId)
  }

  render () {
    return (
      <h3>当前时间：{this.state.date.toLocaleTimeString()}</h3>
    )
  }

  tick () {
    this.setState({
      date: new Date()
    })
  }
}

export default Clock;